export function getUrlQuery(customHref?: string) {
  const url = customHref || window.location.href
  const regex = /[?&]([^=#]+)=([^&#]*)/g
  const paramObj: Record<string, string> = {}

  let match

  while ((match = regex.exec(url))) {
    const key = match[1]
    const value = match[2]
    paramObj[key] = value
  }

  return paramObj
}

// 设置标题
export function setTitle(str: string) {
  ;(document.querySelector('title') as any).innerText = str
}

// 手机号脱敏
export function desensitizePhoneNumber(phone: any) {
  // 假设手机号码为11位数字
  if (phone && phone.length === 11) {
    // 使用正则表达式匹配并替换手机号中间四位为星号
    return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
  }
  return phone // 如果不是有效的手机号，则直接返回原字符串
}
// 身份证号脱敏
export function desensitizeIdCard(idCard: any) {
  // 检查身份证号长度
  if (idCard && idCard.length === 18) {
    // 替换中间十一位为星号
    return idCard.substring(0, 3) + '************' + idCard.substring(14)
  }
  return idCard // 如果不是有效的身份证号，返回原字符串
}

// 时间段处理-去除小时前的0
export function formattedTime(time: any) {
  const formatTime = time.replace(/^0(\d):/, '$1:')
  return formatTime
}

// 处理颜色转换
export function hexToRgb(hexValue: any, opc = 1) {
  if (!hexValue) return ''
  const rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i
  const hex = hexValue.replace(rgx, function (m: any, r: any, g: any, b: any) {
    return r + r + g + g + b + b
  })
  const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
  if (!rgb) {
    return hexValue
  }
  const r = parseInt(rgb[1], 16)
  const g = parseInt(rgb[2], 16)
  const b = parseInt(rgb[3], 16)
  return 'rgba(' + r + ',' + g + ',' + b + ',' + opc + ')'
}

// 权益价值描述类型 突出显示价格
export function importantPrice(str: string): { isImportant: any; value: any }[] {
  str += ''
  const res = str
    .replace(/([\d]+[-][\d]+|￥\d+|￥|\d+)/g, '**$1**')
    .split('**')
    .filter((item) => item)
    .map((item) => {
      item += ''
      return {
        isImportant: item.match(/([\d]+[-][\d]+|￥\d+|￥|\d+)/g),
        value: item,
      }
    })
  return res
}

// cookie
export const setCookie = (name: string, value: any, options: any = {}) => {
  let expires = options.expires

  if (typeof expires == 'number' && expires) {
    const d = new Date()
    d.setTime(d.getTime() + expires * 1000)
    expires = options.expires = d
  }

  if (expires && expires.toUTCString) {
    options.expires = expires.toUTCString()
  }

  value = encodeURIComponent(value)

  let updatedCookie = `${name}=${value}`

  for (const optionKey in options) {
    updatedCookie += `; ${optionKey}`
    const optionValue = options[optionKey]
    if (optionValue !== true) {
      updatedCookie += `=${optionValue}`
    }
  }
  document.cookie = updatedCookie
}

// 当前环境微信
export const isWechatUa = () => {
  const ua = window.navigator.userAgent.toLowerCase()
  return /MicroMessenger/i.test(ua)
}

// 是否是微信小程序
export const isWechatMP = (): any => {
  return (
    (navigator.userAgent.match(/micromessenger/i) && navigator.userAgent.match(/miniprogram/i)) ||
    (window as any).__wxjs_environment === 'miniprogram'
  )
}

// 当前环境不支持外跳
export const localDisablelinkJump = () => {
  const isWxMini = isWechatMP() // 微信小程序
  return isWxMini ? true : false
}

